package per.hongze.javatest.action;

import com.drew.imaging.jpeg.JpegMetadataReader;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.Tag;

import java.io.File;
import java.util.Scanner;

/**
 * java读取照片信息
 */
public class SampleUsage{
    public static void main(String[] args) throws Exception{
        System.out.print("输入要获取坐标的图片路径：");
        Scanner sb = new Scanner(System.in);
        String imagPath = sb.nextLine();
        sb.close();
        if(imagPath.equals("")) System.exit(0);
        File file = new File(imagPath);
        Metadata metadata = JpegMetadataReader.readMetadata(file);
        String longitude = "";
        String latitude = "";
        for(Directory directory : metadata.getDirectories()){
            for(Tag tag : directory.getTags()){
                if(tag.getTagName().equals("GPS Longitude")){
                    longitude = pointToLatlong(tag.getDescription());
                    System.out.print("name : " + "经度(度分秒格式)" + "  -->");
                    System.out.println("desc : " + longitude);
                }else if(tag.getTagName().equals("GPS Latitude")){
                    latitude = pointToLatlong(tag.getDescription());
                    System.out.print("name : " + "纬度(度分秒格式)" + "  -->");
                    System.out.println("desc : " + latitude);
                } else{
                    System.out.print("name : " + tag.getTagName() + "  -->");
                    System.out.println("desc : " + tag.getDescription());
                }
            }
        }
        System.out.println("百度地图使用的坐标为：" + longitude + "," + latitude);
    }

    /**
     * 经纬度格式  转换为  度分秒格式 ,如果需要的话可以调用该方法进行转换
     * @param point 坐标点
     * @return string
     */
    public static String pointToLatlong (String point ) {
        Double du = Double.parseDouble(point.substring(0, point.indexOf("°")).trim());
        Double fen = Double.parseDouble(point.substring(point.indexOf("°")+1, point.indexOf("'")).trim());
        Double miao = Double.parseDouble(point.substring(point.indexOf("'")+1, point.indexOf("\"")).trim());
        Double duStr = du + fen / 60 + miao / 60 / 60 ;
        return duStr.toString();
    }
}